package first.progr.tc;

import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Random;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;

import first.progr.helpstaff.ApplicationManager;
import static first.progr.tc.GroupDataGenerator.generateRandomGroups;
import static first.progr.tc.ContactDataGenerator.generateRandomContacts;;

public class TestBase {

	public ApplicationManager app;
	
	@BeforeTest
	public void setUp() throws Exception {
		String configFile = System.getProperty("configFile", "firefox.properties");
		Properties prop = new Properties();
		prop.load(new FileReader(new File(configFile)));
		app = new ApplicationManager(prop);
	}
	
	@AfterTest
	public void tearDown() throws Exception {
	    app.testStop();
	}
	
	@DataProvider
	public Iterator<Object[]> randomValidGroupGenerator() {
		return wrapGroupsForDataProvider(generateRandomGroups(1)).iterator();    
	}

	public List<Object[]> wrapGroupsForDataProvider(List<GroupData> groupList) {
		List<Object[]> list = new ArrayList<Object[]>();
		for (GroupData gr : groupList){
			list.add(new Object[]{gr});
		}
		return list;
	}

	@DataProvider
	public Iterator<Object[]> randomValidContactGenerator() {
		return wrapContactsForDataProvider(generateRandomContacts(2)).iterator();
	}
	
	public List<Object[]> wrapContactsForDataProvider(List<ContactData> contactList) {
		List<Object[]> list = new ArrayList<Object[]>();
		for (ContactData cont1 : contactList){
			list.add(new Object[]{cont1});
		}
		return list;
	}

	// ----------------------------------------------------------------------

	public static String generateRandStr(){
		Random rnd = new Random();
		if (rnd.nextInt(5) == 0) {
			return "";
		} else {
			return "Data" + rnd.nextInt();
		}
	}	
}
